package com.duowei.diandan.constant;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.duowei.diandan.R;
import com.duowei.diandan.app.App;
import com.duowei.diandan.bean.UserCardInfo;
import com.duowei.diandan.db.entry.Yhjbqk;

/**
 * 设置
 */
public class Settings {
    private static final String TAG = "Settings";

    private static Settings instance;

    /** 重新下载 */
    public static boolean RESTART_DOWNLOAD = false;

    /** 设置-基本-设备名 */
    public static String DEVICE_NAME;
    /** 设置-基本-服务器IP */
    public static String SERVER_IP;
    /** 设置-基本-默认用户 */
    public static Yhjbqk DEFAULT_YHJBQK;
    /** 下载图片 */
    public static boolean DOWNLOAD_IMAGE;
    /** 只显示自助产品 */
    public static boolean ONLY_SHOW_BY21;
    public static boolean HIDE_SETTING_BUTTON;

    /** 设置-功能选择-云会员付款功能 */
    public static boolean CLOUD_FUC;
    /** 设置-功能选择-云会员抵用券可使用张数 */
    public static int CLOUD_TICKET_NUM;
    public static boolean CLOUD_TICKET_DEFAULT_ONE = true;
    /** 设置-功能选择-前台结算功能 */
    // public static boolean COMMIT_FUC;
    /** 设置-功能选择-扫描功能 */
    public static boolean SCAN_FUC;
    public static ScanPayType SCAN_PAY_TYPE;
    public static boolean DISTINGUISH_BETWEEN_MEMBERS;
    public static CloudLoginFavorite CLOUD_LOGIN_FAVORITE;
    public static boolean CLOUD_DETAIL_SHOW_BACK_CART;
    public static boolean MAIN_INTERFACE_SHOW_LABEL;

    /** 设置-界面参数设置-主菜单列数 */
    public static int COLUMN_MENU = 2;

    /** 设置-界面参数设置-单品组列数 */
    public static int COLUMN_SINGLE_GROUP = 3;

    /** 设置-界面参数设置-单品界面-买赠促销、加价促销列数 */
    public static int COLUMN_PROMOTION = 3;

    /** 设置-界面参数设置-、单品界面-套餐列数 */
    public static int COLUMN_COMBO = 3;

    /** 设置-界面参数设置-套餐列表列数 */
    public static int COLUMN_MEAL = 3;

    /*** 餐桌选择界面列数 */
    public static int COLUMN_DESK_CHOICE = 5;

//    /** 设置-界面参数设置-自定义卡片高度 */
//    public static int CARD_HEIGHT;

    public static boolean COUNTDOWN_ENABLED;
    public static int COUNTDOWN_SECOND;

    /** 开启滚动信息 */
    public static boolean ROLL_MESSAGE_ENABLED;
    public static int ROLL_MESSAGE_BG_COLOR;
    public static int ROLL_MESSAGE_FONT_COLOR;
    public static int ROLL_MESSAGE_FONT_SIZE;

    /** 设置-点餐模式设置-点餐模式 */
    public static OrderMode ORDER_MODE = OrderMode.SELF_SERVICE;
    public static PlateMode PLATE_MODE = PlateMode.AUTO;
    public static MealPosition MEAL_POSITION = MealPosition.RECOMMEND;
    public static boolean SEARCH_BAR_ENABLE;

    /** 设置-结账小票设置-打印模式 */
    public static PrintMode PRINTER_MODE;

    public static boolean PRINTER_IP_ENABLE;
    /** 设置-结账小票设置-结账小票打印机IP */
    public static String PRINTER_IP;

    /** 设置-启动界面图片轮播-图片轮播时间间隔 */
    public static int IMAGE_PLAY_TIME;

    // ------------------
    // ------------------
    // ------------------

    /** 用餐类型 */
    public static ChoiceType CHOICE_TYPE = ChoiceType.EAT_IN;

    // ------------------
    // ------------------
    // ------------------

    public static String PLATE_NO;

    /**
     * 自助屏会员模式 保存的云会员信息
     */
    public static UserCardInfo USER_CARD_INFO;

    private Settings() {
    }

    public static Settings getInstance() {
        if (instance == null)
            instance = new Settings();
        return instance;
    }

    /**
     * 更新设置中的数据
     */
    public static void updateSettings() {
        Log.d(TAG, "updateSettings: start");

        Context context = App.getApp();
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        Resources res = context.getResources();

        // --设置--基本--
        DEVICE_NAME = sharedPref.getString(context.getString(R.string.pref_key_device_name), "");

        SERVER_IP = sharedPref.getString(context.getString(R.string.pref_key_server_ip), "");

        String defaultUserCode = sharedPref.getString(context.getString(R.string.pref_key_default_user_code), "");
        String defaultUserName = sharedPref.getString(context.getString(R.string.pref_key_default_user_name), "");
        if (!TextUtils.isEmpty(defaultUserCode) && !TextUtils.isEmpty(defaultUserName)) {
            DEFAULT_YHJBQK = new Yhjbqk();
            DEFAULT_YHJBQK.setYhbh(defaultUserCode);
            DEFAULT_YHJBQK.setYhmc(defaultUserName);
        } else {
            DEFAULT_YHJBQK = null;
        }

        DOWNLOAD_IMAGE = sharedPref.getBoolean(context.getString(R.string.pref_key_download_image), true);
        ONLY_SHOW_BY21 = sharedPref.getBoolean(context.getString(R.string.pref_key_only_show_by21), false);

        HIDE_SETTING_BUTTON = sharedPref.getBoolean(context.getString(R.string.pref_key_hide_setting_button), false);

        // --设置--功能选择--

        CLOUD_FUC = sharedPref.getBoolean(context.getString(R.string.pref_key_cloud_function), true);
        String num = sharedPref.getString(context.getString(R.string.pref_key_cloud_ticket_num), "0");
        try {
            CLOUD_TICKET_NUM = Integer.valueOf(num);
        } catch (NumberFormatException e) {
            CLOUD_TICKET_NUM = 0;
        }

        CLOUD_TICKET_DEFAULT_ONE = sharedPref.getBoolean(context.getString(R.string.pref_key_cloud_ticket_default_one), true);

        SCAN_FUC = sharedPref.getBoolean(context.getString(R.string.pref_key_scan_function), false);

        SCAN_PAY_TYPE = ScanPayType.valueOf(sharedPref.getString(context.getString(R.string.pref_key_scan_pay_type), "SELF_TYPE"));

        DISTINGUISH_BETWEEN_MEMBERS = sharedPref.getBoolean(context.getString(R.string.pref_key_distinguish_between_members), false);

        CLOUD_LOGIN_FAVORITE = CloudLoginFavorite.valueOf(sharedPref.getString(context.getString(R.string.pref_key_cloud_login_favorite), "SCAN_LOGIN"));

        CLOUD_DETAIL_SHOW_BACK_CART = sharedPref.getBoolean(context.getString(R.string.pref_key_cloud_detail_show_back_cart), false);

        MAIN_INTERFACE_SHOW_LABEL = sharedPref.getBoolean(context.getString(R.string.pref_key_main_interface_show_label), false);

        COLUMN_MENU = Integer.valueOf(sharedPref.getString(
                context.getString(R.string.pref_key_column_menu), "2"));

        COLUMN_SINGLE_GROUP = Integer.valueOf(sharedPref.getString(
                context.getString(R.string.pref_key_column_single_group), "3"));

        COLUMN_PROMOTION = Integer.valueOf(sharedPref.getString(
                context.getString(R.string.pref_key_column_promotion), "3"));

        COLUMN_COMBO = Integer.valueOf(sharedPref.getString(
                context.getString(R.string.pref_key_column_combo), "3"));

        COLUMN_MEAL = Integer.valueOf(sharedPref.getString(
                context.getString(R.string.pref_key_column_meal), "3"));

        COLUMN_DESK_CHOICE = Integer.valueOf(sharedPref.getString(
                context.getString(R.string.pref_key_column_desk_choice), "5"));

        ORDER_MODE = OrderMode.valueOf(sharedPref.getString(
                context.getString(R.string.pref_key_order_mode), "SELF_SERVICE"));

        PLATE_MODE = PlateMode.valueOf(sharedPref.getString(
                context.getString(R.string.pref_key_plate_mode), "AUTO"));

        MEAL_POSITION = MealPosition.valueOf(sharedPref.getString(context.getString(R.string.pref_key_meal_position), "RECOMMEND"));

        SEARCH_BAR_ENABLE = sharedPref.getBoolean(context.getString(R.string.pref_key_search_bar_enable), false);

        // --设置--结账小票设置--
        PRINTER_MODE = PrintMode.valueOf(sharedPref.getString(
                context.getString(R.string.pref_key_print_mode), "INSIDE_MODE"));

        PRINTER_IP_ENABLE = sharedPref.getBoolean(context.getString(R.string.pref_key_printer_ip_enable), false);
        PRINTER_IP = sharedPref.getString(context.getString(R.string.pref_key_printer_ip), "");

        // --设置--启动界面图片轮播--
        IMAGE_PLAY_TIME = Integer.valueOf(sharedPref.getString(context.getString(R.string.pref_key_image_play_time), "5"));

        // --设置--开启倒计时
        COUNTDOWN_ENABLED = sharedPref.getBoolean(context.getString(R.string.pref_key_countdown_enabled), true);

        ROLL_MESSAGE_ENABLED = sharedPref.getBoolean(context.getString(R.string.pref_key_roll_message_enabled), false);
        ROLL_MESSAGE_BG_COLOR = sharedPref.getInt(context.getString(R.string.pref_key_roll_message_bg_color), Color.WHITE);
        ROLL_MESSAGE_FONT_COLOR = sharedPref.getInt(context.getString(R.string.pref_key_roll_message_font_color), Color.BLACK);
        ROLL_MESSAGE_FONT_SIZE = Integer.valueOf(sharedPref.getString(context.getString(R.string.pref_key_roll_message_font_size), "16"));

        // --设置--倒计时
        String second = sharedPref.getString(context.getString(R.string.pref_key_countdown_second), "60");
        try {
            COUNTDOWN_SECOND = Integer.valueOf(second);
        } catch (NumberFormatException e) {
            COUNTDOWN_SECOND = 60;
        }

        Log.d(TAG, "updateSettings: end");
    }

    /**
     * 检查设置中必填参数
     *
     * @param showMsg true 显示消息
     * @return true, 都设置; false,未全部设置
     */
    public static boolean checkSettings(boolean showMsg) {
        if (TextUtils.isEmpty(DEVICE_NAME)) {
            if (showMsg) showMsg("请设置设备名");
            return false;
        }

        if (TextUtils.isEmpty(SERVER_IP)) {
            if (showMsg) showMsg("请设置服务器IP");
            return false;
        }

        if (DEFAULT_YHJBQK == null) {
            if (showMsg) showMsg("请设置默认用户");
            return false;
        }

        return true;
    }

    private static void showMsg(String msg) {
        Toast.makeText(App.getApp(), msg, Toast.LENGTH_SHORT).show();
    }

    // ----------------------------------
    // ----------------------------------
    // ----------------------------------

    /**
     * 设置默认用户
     *
     * @param defaultUser
     */
    public static void setDefaultUser(Yhjbqk defaultUser) {
        DEFAULT_YHJBQK = defaultUser;

        Context context = App.getApp();
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        if (defaultUser == null) {
            editor.putString(context.getString(R.string.pref_key_default_user_code), "");
            editor.putString(context.getString(R.string.pref_key_default_user_name), "");

        } else {
            editor.putString(context.getString(R.string.pref_key_default_user_code), defaultUser.getYhbh());
            editor.putString(context.getString(R.string.pref_key_default_user_name), defaultUser.getYhmc());
        }

        editor.apply();
    }
}
